﻿using GrapeCity.Forguncy.Commands;
using GrapeCity.Forguncy.Plugin;
using Neo4JConnector.Core;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Neo4JConnector
{
    [Icon("pack://application:,,,/Neo4JConnector;component/Resources/Icon.png")]
    [Category("neo4j图数据库")]
    [OrderWeight(110)]
    public class DeleteNode : BaseNeo4JOp
    {

        [FormulaProperty]
        [DisplayName("被删除节点的标签")]
        [Description("类似于数据库中的表，用于对节点进行分类。")]
        [OrderWeight(100)]
        [SearchableProperty]
        public object LabelExpr { get; set; }

        [FormulaProperty]
        [DisplayName("被删除节点的ID")]
        [Description("类似于数据库中的行ID，在同一个表内需要保持唯一。")]
        [OrderWeight(101)]
        [SearchableProperty]
        public object IDExpr { get; set; }


        public override async Task<ExecuteResult> ExecuteAsync(IServerCommandExecuteContext dataContext)
        {
            var label = (await dataContext.EvaluateFormulaAsync(LabelExpr))?.ToString() ?? "";
            var id = (await dataContext.EvaluateFormulaAsync(IDExpr))?.ToString() ?? null;

            if (string.IsNullOrEmpty(label) || string.IsNullOrEmpty(id))
            {
                throw new Exception("被删除节点的标签、ID不能为空。");
            }

            if (!Validators.IsValidLabel(label))
                throw new Exception("节点的标签包含非法字符，仅支持字母数字和下划线，且需要以字母开头。");

            var client = await getClient(dataContext);

            await client.DeleteEntity(label, id);

            return new ExecuteResult();
        }

        public override string ToString()
        {
            return "neo4j：删除节点";
        }
    }
}
